home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / 68hc11 / smallc11.arc / CC31.C < prev    next >
Text File  |  1988-05-27  |  9KB  |  356 lines

  1. /*
  2. ** lval[0] - symbol table address, else 0 for constant
  3. ** lval[1] - type of indirect obj to fetch, else 0 for static
  4. ** lval[2] - type of pointer or array, else 0 for all other
  5. ** lval[3] - true if constant expression
  6. ** lval[4] - value of constant expression
  7. ** lval[5] - true if secondary register altered
  8. ** lval[6] - function address of highest/last binary operator
  9. ** lval[7] - stage address of "oper 0" code, else 0
  10. */
  11.  
  12. /*
  13. ** skim over terms adjoining || and && operators
  14. */
  15. skim(opstr, testfunc, dropval, endval, heir, lval)
  16.   char *opstr;
  17.   int testfunc, dropval, endval, heir, lval[]; {
  18.   int k, hits, droplab, endlab;
  19.   hits=0;
  20.   while(1) {
  21.     k=plunge1(heir, lval);
  22.     if(nextop(opstr)) {
  23.       bump(opsize);
  24.       if(hits==0) {
  25.     hits=1;
  26.     droplab=getlabel();
  27.     }
  28.       dropout(k, testfunc, droplab, lval);
  29.       }
  30.     else if(hits) {
  31.       dropout(k, testfunc, droplab, lval);
  32.       const1(endval);
  33.       jump(endlab=getlabel());
  34.       postlabel(droplab);
  35.       const1(dropval);
  36.       postlabel(endlab);
  37.       lval[1]=lval[2]=lval[3]=lval[7]=0;
  38.       return 0;
  39.       }
  40.     else return k;
  41.     }
  42.   }
  43.  
  44. /*
  45. ** test for early dropout from || or && evaluations
  46. */
  47. dropout(k, testfunc, exit1, lval) int k, (*testfunc)(), exit1, lval[]; {
  48.   if(k) rvalue(lval);
  49.   else if(lval[3]) const1(lval[4]);
  50.   (*testfunc)(exit1); /* jumps on false */
  51.   }
  52.  
  53. /*
  54. ** plunge to a lower level
  55. */
  56. plunge(opstr, opoff, heir, lval)
  57.   char *opstr;
  58.   int opoff, heir, lval[]; {
  59.   int k, lval2[8];
  60.   k=plunge1(heir, lval);
  61.   if(nextop(opstr)==0) return k;
  62.   if(k) rvalue(lval);
  63.   while(1) {
  64.     if(nextop(opstr)) {
  65.       bump(opsize);
  66.       opindex=opindex+opoff;
  67.       plunge2(op[opindex], op2[opindex], heir, lval, lval2);
  68.       }
  69.     else return 0;
  70.     }
  71.   }
  72.  
  73. /*
  74. ** unary plunge to lower level
  75. */
  76. plunge1(heir, lval) int (*heir)(), lval[]; {
  77.   char *before, *start;
  78.   int k;
  79.   setstage(&before, &start);
  80.   k=(*heir)(lval);
  81.   if(lval[3]) clearstage(before,0); /* load constant later */
  82.   return k;
  83.   }
  84.  
  85. /*
  86. ** binary plunge to lower level
  87. */
  88. plunge2(oper, oper2, heir, lval, lval2)
  89. int  (*oper)(), (*oper2)(), (*heir)(), lval[], lval2[];
  90. {
  91. char *before, *start;
  92.  
  93. setstage(&before, &start);
  94. lval[5]=1;        /* flag secondary register used */
  95. lval[7]=0;        /* flag as not "... oper 0" syntax */
  96. if(lval[3]) {        /* constant on left side not yet loaded */
  97.     if(plunge1(heir, lval2)) rvalue(lval2);
  98.     if(lval[4]==0) lval[7]=stagenext;
  99.     const2(lval[4]<<dbltest(lval2, lval));
  100.     }
  101. else {            /* non-constant on left side */
  102.     push();
  103.     if(plunge1(heir, lval2)) rvalue(lval2);
  104.     if(lval2[3]) {    /* constant on right side */
  105.       if(lval2[4]==0) lval[7]=start;
  106.       if(oper == op[ADD]) { /* may test other commutative operators */
  107.     csp=csp+2;
  108.     clearstage(before, 0);
  109.     const2(lval2[4]<<dbltest(lval, lval2));   /* load secondary */
  110.     }
  111.       else {
  112.     const1(lval2[4]<<dbltest(lval, lval2));      /* load primary */
  113.     smartpop(lval2, start);
  114.     }
  115.       }
  116.     else {          /* non-constants on both sides */
  117.       smartpop(lval2, start);
  118.       if((oper == op[ADD])|(oper == op[SUB])) {
  119.     if(dbltest(lval,lval2)) doublereg();
  120.     if(dbltest(lval2,lval)) {
  121.       swap();
  122.       doublereg();
  123.       if(oper == op[SUB]) swap();
  124.       }
  125.     }
  126.       }
  127.     }
  128.   if(oper) {
  129.     if(lval[3]=lval[3]&lval2[3]) {
  130.       lval[4]=calc(lval[4], oper, lval2[4]);
  131.       clearstage(before, 0);
  132.       lval[5]=0;
  133.       }
  134.     else {
  135.       if((lval[2]==0)&(lval2[2]==0)) {
  136.     (*oper)();
  137.     lval[6]=oper;     /* identify the operator */
  138.     }
  139.       else {
  140.     (*oper2)();
  141.     lval[6]=oper2;     /* identify the operator */
  142.     }
  143.       }
  144.     if(oper == op[SUB]) {
  145.       if((lval[2]==CINT)&(lval2[2]==CINT)) {
  146.     swap();
  147.     const1(1);
  148.     asr();    /** div by 2 **/
  149.     }
  150.       }
  151.     if((oper == op[SUB])|(oper == op[ADD])) result(lval, lval2);
  152.     }
  153.   }
  154.  
  155. calc(left, oper, right) int left, oper, right; {
  156.        if(oper ==  op[OR]) return (left  |  right);
  157.   else if(oper == op[XOR]) return (left  ^  right);
  158.   else if(oper == op[AND]) return (left  &  right);
  159.   else if(oper ==  op[EQ]) return (left  == right);
  160.   else if(oper ==  op[NE]) return (left  != right);
  161.   else if(oper ==  op[LE]) return (left  <= right);
  162.   else if(oper ==  op[GE]) return (left  >= right);
  163.   else if(oper ==  op[LT]) return (left  <  right);
  164.   else if(oper ==  op[GT]) return (left  >  right);
  165.   else if(oper == op[ASR]) return (left  >> right);
  166.   else if(oper == op[ASL]) return (left  << right);
  167.   else if(oper == op[ADD]) return (left  +  right);
  168.   else if(oper == op[SUB]) return (left  -  right);
  169.   else if(oper ==op[MULT]) return (left  *  right);
  170.   else if(oper == op[DIV]) return (left  /  right);
  171.   else if(oper == op[MOD]) return (left  %  right);
  172.   else return 0;
  173.   }
  174.  
  175. expression(const1, val) int *const1, *val;  {
  176.   int lval[8];
  177.   if(heir1(lval)) rvalue(lval);
  178.   if(lval[3]) {
  179.     *const1=1;
  180.     *val=lval[4];
  181.     }
  182.   else *const1=0;
  183.   }
  184.  
  185. heir13(lval)  int lval[];  {
  186.     int k,inc(),dec();
  187.   char *ptr;
  188.   if(match("++")) {                   /* ++lval */
  189.     if(heir13(lval)==0) {
  190.       needlval();
  191.       return 0;
  192.       }
  193.     step(inc, lval);
  194.     return 0;
  195.     }
  196.   else if(match("--")) {              /* --lval */
  197.     if(heir13(lval)==0) {
  198.       needlval();
  199.       return 0;
  200.       }
  201.     step(dec, lval);
  202.     return 0;
  203.     }
  204.   else if (match("~")) {              /* ~ */
  205.     if(heir13(lval)) rvalue(lval);
  206.     com();
  207. #ifdef PHASE2
  208.     lval[4] = ~lval[4];
  209. #endif
  210.     return 0;
  211.     }
  212.   else if (match("!")) {              /* ! */
  213.     if(heir13(lval)) rvalue(lval);
  214.     lneg();
  215. #ifdef PHASE2
  216.     lval[4] = !lval[4];
  217. #endif
  218.     return 0;
  219.     }
  220.   else if (match("-")) {              /* unary - */
  221.     if(heir13(lval)) rvalue(lval);
  222.     neg();
  223.     lval[4] = -lval[4];
  224.     return 0;
  225.     }
  226.   else if(match("*")) {               /* unary * */
  227.     if(heir13(lval)) rvalue(lval);
  228.     if(ptr=lval[0])lval[1]=ptr[TYPE];
  229.     else lval[1]=CINT;
  230.     lval[2]=0;    /* flag as not pointer or array */
  231.     lval[3]=0;    /* flag as not constant */
  232.     return 1;
  233.     }
  234.   else if(match("&")) {               /* unary & */
  235.     if(heir13(lval)==0) {
  236.       error("illegal address");
  237.       return 0;
  238.       }
  239.     ptr=lval[0];
  240.     lval[2]=ptr[TYPE];
  241.     if(lval[1]) return 0;
  242.     /* global & non-array */
  243.     address(ptr);
  244.     lval[1]=ptr[TYPE];
  245.     return 0;
  246.     }
  247.   else {
  248.     k=heir14(lval);
  249.     if(match("++")) {                 /* lval++ */
  250.       if(k==0) {
  251.     needlval();
  252.     return 0;
  253.     }
  254.       step(inc, lval);
  255.       dec(lval[2]>>2);
  256.       return 0;
  257.       }
  258.     else if(match("--")) {            /* lval-- */
  259.       if(k==0) {
  260.     needlval();
  261.     return 0;
  262.     }
  263.       step(dec, lval);
  264.       inc(lval[2]>>2);
  265.       return 0;
  266.       }
  267.     else return k;
  268.     }
  269.   }
  270.  
  271. heir12(lval)  int lval[];  {
  272.   return plunge("* / %", 13, heir13, lval);
  273.   }
  274.  
  275. heir11(lval)  int lval[];  {
  276.   return plunge("+ -", 11, heir12, lval);
  277.   }
  278.  
  279. heir10(lval)  int lval[];  {
  280.   return plunge(">> <<", 9, heir11, lval);
  281.   }
  282.  
  283. heir9(lval)  int lval[];  {
  284.   return plunge("<= >= < >", 5, heir10, lval);
  285.   }
  286.  
  287. heir8(lval)  int lval[];  {
  288.   return plunge("== !=", 3, heir9, lval);
  289.   }
  290.  
  291. heir7(lval)  int lval[]; {
  292.   return plunge("&", 2, heir8, lval);
  293.   }
  294.  
  295. heir6(lval)  int lval[]; {
  296.   return plunge("^", 1, heir7, lval);
  297.   }
  298.  
  299. heir5(lval)  int lval[]; {
  300.   return plunge("|", 0, heir6, lval);
  301.   }
  302.  
  303. heir4(lval)  int lval[]; {
  304.     int hier5(),ne0();
  305.   return skim("&&", ne0, 0, 1, heir5, lval);
  306.   }
  307.  
  308. heir3(lval)  int lval[]; {
  309.     int hier4(),eq0();
  310.   return skim("||", eq0, 1, 0, heir4, lval);
  311.   }
  312.  
  313. heir1(lval)  int lval[];  {
  314.   int k,lval2[8], oper;
  315.   k=plunge1(heir3, lval);
  316.   if(lval[3]) const1(lval[4]);
  317.        if(match("|="))  oper=or;
  318.   else if(match("^="))  oper=xor;
  319.   else if(match("&="))  oper=and;
  320.   else if(match("+="))  oper=add;
  321.   else if(match("-="))  oper=sub;
  322.   else if(match("*="))  oper=mult;
  323.   else if(match("/="))  oper=div;
  324.   else if(match("%="))  oper=mod;
  325.   else if(match(">>=")) oper=asr;
  326.   else if(match("<<=")) oper=asl;
  327.   else if(match("="))   oper=0;
  328.   else return k;
  329.   if(k==0) {
  330.     needlval();
  331.     return 0;
  332.     }
  333.   if(lval[1]) {
  334.     if(oper) {
  335.       push();
  336.       rvalue(lval);
  337.       }
  338.     plunge2(oper, oper, heir1, lval, lval2);
  339.     if(oper) pop();
  340.     }
  341.   else {
  342.     if(oper) {
  343.       rvalue(lval);
  344.       plunge2(oper, oper, heir1, lval, lval2);
  345.       }
  346.     else {
  347.       if(heir1(lval2)) rvalue(lval2);
  348.       lval[5]=lval2[5];
  349.       }
  350.     }
  351.   store(lval);
  352.   return 0;
  353.   }
  354.  
  355.  
  356.